﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCPnuts.ISkyDiskFileSystem.Interfaces;
using CCPnuts.Pack;
using CCPnuts.ISkyDiskFileSystem;

namespace TestSkyDiskFileSystem
{
    class SkyElementCollection : List<I_SkyElement>, I_SkyElementCollection
    {
        #region IPacked Members

        public PackedInfo m_packedInfo = PackedInfos.SkyElementCollection;

        public PackedInfo _PackedInfo { get { return m_packedInfo; } }

        #endregion

        #region Static Methods for gene random file system.

        public static SkyElementCollection Gene(SkyElementContainer parent, Random ran, int level)
        {
            var col = new SkyElementCollection();
            if (level > 0)
            {
                int dirCount = ran.Next(0, 4);
                int fileCount = ran.Next(0, 6);
                for (int i = 0; i < dirCount; i++)
                {
                    var dir = new SkyDirectory { Name = GeneName(ran),Parent = parent };
                    col.Add(dir);
                    dir.Children = Gene(dir, ran, level - 1);
                }
                for (int i = 0; i < fileCount; i++)
                {
                    col.Add(new SkyFile
                    {
                        Name = GeneName(ran)
                    });
                }
            }
            return col;
        }

        public static SkyElementCollection Gene(SkyElementContainer parent, string name)
        {
            return Gene(parent, name, 5);
        }

        public static SkyElementCollection Gene(SkyElementContainer parent, string name, int level)
        {
            int seed = 0;
            foreach (var c in name)
            {
                seed += (int)c;
            }
            Random ran = new Random(seed);
            return Gene(parent, ran, level);
        }

        static string GeneName(Random ran)
        {
            int count = ran.Next(3, 8);
            byte[] nameB = new byte[count];
            for (int j = 0; j < count; j++)
            {
                nameB[j] = (byte)ran.Next((int)'a', (int)'z');
            }
            return Encoding.ASCII.GetString(nameB);
        }

        #endregion
    }

    static class ISkyElementCollectionExtend
    {
        public static List<I_SkyElement> ToList(this I_SkyElementCollection coll)
        {
            return coll as List<I_SkyElement>;
        }
    }
}
